package core.hash;

import java.util.ArrayList;
import java.util.List;

import core.util.CommUtils;
import core.util.Const;
/**
 * hash算工具类
 * @author qiwx
 *          
 */
public class ConsistHashUtil {
  
  private static ConsistencyHash dataSouceHash;
  
  static {
    dataSouceHash = new ConsistencyHash();
  }
  /**
   * 初始化数据源的hash
   * @param dataSourceList
   */
  public static void init( List<String> dataSourceList ) {
    dataSouceHash.init(dataSourceList);
  }
  
  /**
   * 根据bid来获得数据源
   * @param bid
   * @return
   */
  public static String getDataSource( String bid ) {
    String dataSource = dataSouceHash.getShardInfo( bid );
    if ( CommUtils.isNull(dataSource) ) {
      return "";
    }
    return dataSouceHash.getShardInfo( bid );
  }
  
  public static void main(String[] args) {
    List<String> dataSourceList = new ArrayList<String>();
    dataSourceList.add("test1");
    dataSourceList.add("test2");
    dataSourceList.add("test3");
    ConsistHashUtil.init(dataSourceList);
    String dd1 = ConsistHashUtil.getDataSource("AAAAAAAAAAAAAA-"+ 1);
    String dd2 = ConsistHashUtil.getDataSource("AAAAAAAAAAAAAA-"+ 2);
    String dd3 = ConsistHashUtil.getDataSource("AAAAAAAAAAAAAA-"+ 3);
    String dd4 = ConsistHashUtil.getDataSource("AAAAAAAAAAAAAA-"+ 4);
    System.out.println( dd1 );
    System.out.println( dd2 );
    System.out.println( dd3 );
    System.out.println( dd4 );
  }
  
}
